<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let name = ['小明', '小蓝', '小红']
    let num = [1, 2, 3, 4, 5, 6]
    /* 
      在之前是创建一个专门用来针对数组创建迭代器对象的函数，用不同的迭代器来迭代不同的数组的，
      现在改用生成器代替迭代器，同样达到相同的效果
    
    */

    function* createArrIterator(arr) {
      for (let i = 0; i < arr.length; i++) {
        yield arr[i]
      }
    }

    let nameIterator = createArrIterator(name)
    let numIterator = createArrIterator(num)

    // console.log(nameIterator.next());
    // console.log(nameIterator.next());
    // console.log(nameIterator.next());
    // console.log(nameIterator.next());
    // console.log(nameIterator.next());

    // 使用生成器返回一个范围内的值
    function* createRange(start, end) {
      for (let i = start; i < end; i++) {
        yield i
      }
    }

    let genderator = createRange(3, 9)
    console.log(genderator.next());
    console.log(genderator.next());
    console.log(genderator.next());
    console.log(genderator.next());
    console.log(genderator.next());
    console.log(genderator.next());
    console.log(genderator.next());
    console.log(genderator.next());
  </script>

</body>

</html>